﻿<?php

/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-4-8
*    This class designed  for the news operation
*/
class member {
	private $conn;
	private $error;
	private $url;

	function __construct($url) {

		/*
		 * load connection class
		 */
		include_once $url . 'includes/conn.php';
         $this->set_url($url);

	}

	/*
	 * defin the function for member add
	 *
	 * $fun=0 means update
	 * $fun=1 means add
	 *
	 */
function member_add($id,$name,$pass,$group,$mail,$fun){
	if(!empty($pass)){
     $pass=sha1($pass);}
$conn = new conn();
		if ($fun) {
			/*
			 * user add codes here
			 */
			$sql = sprintf("insert into users(id,name,pass,groups,E_mail)values(null,'%s','%s','%d','%s')", $name, $pass,$group,$mail);
		} else {
			/*
			 * user update codes here
			 */
			 if(!empty($group)){
			$sql = sprintf("update users set name='%s',groups='%d' ,pass='%s' where id='%d'", $name,$group,$pass,$id);
		}else{
				$sql = sprintf("update users set name='%s',pass='%s' where id='%d'", $name,$pass,$id);

		}
			 }
		echo $sql;
		$conn->set_sql($sql);
		$line = $conn->get_line();
		return $line;

}
/*
 *  member del
 *
 */
 function member_del($ids){
	if (isset ($ids)) {
			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i < $max; $i++) {
					$conn = new conn();
					$sql = sprintf("delete from users where id=%d ; ", $ids[$i]);
					$conn->set_sql($sql);
					$line1 = $conn->get_line();
					$line += $line1;
				}
			} else {
				$conn = new conn();
				$sql = sprintf("delete from users  where id=%d", $ids);
				$conn->set_sql($sql);
				$line = $conn->get_line();
			}
		} else {
			echo 'at least choose one for process please';
			$line = 0;
		}
		return $line;


 }

 /*
  * member read
  */
  function member_read($id){
  $url=$this->get_url();

  include_once $url.'includes/entity/users.entity.php';
    $conn = new conn();
		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from users limit %d,%d ", $a, $b);
		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from users where id='%d' ", $id);
		}
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$users[$i] = new users();
				$id = $row['id'];
				$name = $row['name'];
				$pass = $row['pass'];
				$group = $row['groups'];
				$mail  = $row['E_mail'];
				 $users[$i]->set_id($id);
				 $users[$i]->set_name($name);
				 $users[$i]->set_pass($pass);
				 $users[$i]->set_group($group);
				 $users[$i]->set_E_mail($mail);
			}
		}
		return $users;


  }

/*
 * right modified function
 *
 */
 function right($id,$name,$right){



 }
	/*
	 * functions for use above
	 */

	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}
}
?>